<?php 
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-06-29 22:42:45 +0200 (Sun, 29 Jun 2008) $
 * $Rev: 1240 $
 * $Author: gavinspearhead $
 * $Id: ajax_edittransfers.php 1240 2008-06-29 20:42:45Z gavinspearhead $
 */
define('ORIGINAL_PAGE', $_SERVER['PHP_SELF']);
$__auth = 'silent';

$pathaet = realpath(dirname(__FILE__));

require_once "$pathaet/../functions/functions.php";
require_once "$pathaet/../functions/html_includes.php";

$cmd = get_request('cmd');
$dlid = get_request('dlid');

$db->escape($cmd);
$db->escape($dlid);

if (!is_numeric($dlid))
	die_html('Invalid download ID');

$prefs = load_config($db);
$uc = new urdd_client($prefs['urdd_host'], $prefs['urdd_port'], $userID);


switch (strtolower($cmd)) {
case 'reparrar':
	verify_challenge_text($_POST['challenge']);
	try {
		$uc->unpar_unrar($dlid);
	} catch (exception $e) {
		die_html($LN['error_urddconnect']);
    }
    break;

case 'start' : 
	verify_challenge_text($_POST['challenge']);
	// In case it's paused, continue it:
	try {
		$uc->continue_cmd(get_command(COMMAND_DOWNLOAD_ACTION) . " $dlid");
		$uc->continue_cmd(get_command(COMMAND_DOWNLOAD) . " $dlid");
	} catch (exception $e) {
		die_html($LN['error_urddconnect']);
	}
	break;
case 'pause' : 
	verify_challenge_text($_POST['challenge']);
	try {
		$uc->pause(get_command(COMMAND_DOWNLOAD_ACTION) . ' ' . $dlid);  // Cancel it, just in case; we need both as download is on queue only and download action can be queued and running
		$uc->pause(get_command(COMMAND_DOWNLOAD) . ' ' . $dlid);  // Cancel it, just in case
	} catch (exception $e) {
		die_html($LN['error_urddconnect']);
	}
	break;
case 'cancel' :
	verify_challenge_text($_POST['challenge']);
	try {
		$uc->cancel(get_command(COMMAND_DOWNLOAD_ACTION) . ' ' . $dlid);  // Cancel it, just in case; see pauso
		$uc->cancel(get_command(COMMAND_DOWNLOAD) . ' ' . $dlid);  // Cancel it, just in case
	} catch (exception $e) {
		die_html($LN['error_urddconnect']);
	}
	break;
case 'delete' :
	verify_challenge_text($_POST['challenge']);
	try {
		$uc->cancel(get_command(COMMAND_DOWNLOAD_ACTION) . ' ' . $dlid);  // Cancel it, just in case; see pauso
		$uc->cancel(get_command(COMMAND_DOWNLOAD) . ' ' . $dlid);  // Cancel it, just in case
	} catch (exception $e) {
		die_html($LN['error_urddconnect']);
	}
	if ($isadmin) {
		// Admins can delete any download
		$sql = "DELETE FROM downloadinfo WHERE \"ID\" = '$dlid'";
		$db->execute_query($sql);
		$sql = "DELETE FROM downloadarticles WHERE \"downloadID\" = '$dlid'";
		$db->execute_query($sql);
	} else {
		$username = get_username($db, $userID);
		$db->escape($username);
		$sql = "SELECT * FROM downloadinfo WHERE \"username\" = '$username' AND \"ID\" = '$dlid'";
		$res = $db->execute_query($sql);
		if ($res[0]['ID'] == $dlid) {
			$sql = "DELETE FROM downloadinfo WHERE \"ID\" = '$dlid'";
			$db->execute_query($sql);
			$sql = "DELETE FROM downloadarticles WHERE \"downloadID\" = '$dlid'";
			$db->execute_query($sql);
		}
	}
	break;
case 'showrename' :
	// Get download info:
	$sql = "SELECT * FROM downloadinfo WHERE \"ID\" = '$dlid'";
	$res = $db->execute_query($sql);
	$oldname = $res[0]['name'];
	$oldpw = $res[0]['password'];
	$oldunpar = $res[0]['unpar'];
	$oldunrar = $res[0]['unrar'];
	$oldsubdl = $res[0]['subdl'];
    $olddelete = $res[0]['delete_files'];
    $status = $res[0]['status'];
    $start_time = $res[0]['start_time'];
    if (in_array($status, array(DOWNLOAD_READY, DOWNLOAD_QUEUED, DOWNLOAD_PAUSED)) && $start_time > time()) {
        $smarty->assign('starttime_noedit',	0);
    } else
        $smarty->assign('starttime_noedit',	1);

	$startdate = date('Y-m-d', $start_time);
	$starttime = date('H:i:s', $start_time);

	$smarty->assign('starttime', "$startdate $starttime");
	$challenge = set_challenge();
	$smarty->assign('challenge',    	$challenge);
	$smarty->assign('id',		$dlid);
	$smarty->assign('oldname',	$oldname);
	$smarty->assign('oldpw',	$oldpw);
	$smarty->assign('oldunrar',	$oldunrar);
	$smarty->assign('oldsubdl',	$oldsubdl);
	$smarty->assign('oldunpar',	$oldunpar);
	$smarty->assign('olddelete',	$olddelete);
	$smarty->display('ajax_edittransfers.tpl');
	die;
	break;
case 'rename':
    // Actually rename the download
	verify_challenge_text($_POST['challenge']);
	$newname = trim($_POST['dlname']);
	$db->escape($newname);
    $newpass = trim($_POST['dlpass']);
    $newstarttime = strtotime(trim($_POST['starttime']));
    $sql = "SELECT start_time FROM downloadinfo WHERE \"ID\" = '$dlid'";
    $res = $db->execute_query($sql);
    if (!isset($res[0]['start_time']))
        throw new exception ("Download not found XXX");
    $oldstarttime = $res[0]['start_time'];
    $start_time = NULL;
    if ($newstarttime !== FALSE) {
        $uc->unschedule(get_command(COMMAND_CONTINUE), '"'  . get_command(COMMAND_DOWNLOAD) . " $dlid\""); 
        if ($newstarttime > time()) {
            $start_time = $newstarttime;
            $uc->schedule(get_command(COMMAND_CONTINUE), '"' . get_command(COMMAND_DOWNLOAD) . " $dlid\"", $_POST['starttime']); 
        } else if ($oldstartime > time()) {// if the start is in the future we have to start it if the new start time isn't
            $uc->continue(get_command(COMMAND_DOWNLOAD), $dlid); 
            $start_time = $time();
        } 
    }
    $db->escape($newpass);
	$unpar = (isset ($_POST['unpar']) && $_POST['unpar'] == 'on') ? 1 : 0;
	$unrar = (isset ($_POST['unrar']) && $_POST['unrar'] == 'on') ? 1 : 0;
	$subdl = (isset ($_POST['subdl']) && $_POST['subdl'] == 'on') ? 1 : 0;
	$delete = (isset ($_POST['delete']) && $_POST['delete'] == 'on') ? 1 : 0;
    $sql = "UPDATE downloadinfo SET \"name\" = '$newname', \"password\" = '$newpass', \"unrar\"= '$unrar', \"subdl\"= '$subdl', ";
    if ($start_time !== NULL)
        $sql .= "\"start_time\" = '$start_time', ";

	$sql .= "\"unpar\"='$unpar', \"delete_files\"='$delete' WHERE \"ID\" = '$dlid'";
	$res = $db->execute_query($sql);
	break;
case 'move_up':
	verify_challenge_text($_POST['challenge']);
	try {
		$uc->move_cmd('UP', get_command(COMMAND_DOWNLOAD_ACTION), $dlid);
		$uc->move_cmd('UP', get_command(COMMAND_DOWNLOAD), $dlid);
	} catch (exception $e) {
		die_html($LN['error_urddconnect']);
	}
	break;
case 'move_down':
	verify_challenge_text($_POST['challenge']);
	try {
		$uc->move_cmd('DOWN', get_command(COMMAND_DOWNLOAD_ACTION), $dlid);
		$uc->move_cmd('DOWN', get_command(COMMAND_DOWNLOAD), $dlid);
	} catch (exception $e) {
		die_html($LN['error_urddconnect']);
	}
	break;

default:	
	die_html('Invalid command!');
}


// Success:
die_html('OK');
?>
